package com.company.linkedlist;

/**
 * @author shenhan
 * @create 2020/1/21 14:17
 * @description 不考虑序号添加英雄，实现最终的按序添加
 */
public class SingleLinkedListDemo {

    /**
     * 反转单向链表
     * @param head
     */
    public static void reverseList(HeroNode head) {
        // 如果当前链表为空，或者只有一个节点，无需反转
        if (head.next == null || head.next.next == null) {
            return;
        }
        // 定义一个指针，帮助遍历原来的链表
        HeroNode cur = head.next;
        HeroNode next = null;
        HeroNode reverseHead = new HeroNode(0,"","");
        // 遍历原来的链表
        while (cur != null) {
            next = cur.next;
            cur.next = reverseHead.next;
            reverseHead.next = cur;
            cur = next;
        }
        head.next = reverseHead.next;
    }

    public static void main(String[] args) {
        //先创建几个节点
        final HeroNode node1 = new HeroNode(1, "宋江", "及时雨");
        final HeroNode node2 = new HeroNode(2, "卢俊义", "玉麒麟");
        final HeroNode node3 = new HeroNode(3, "吴用", "智多星");
        final HeroNode node4 = new HeroNode(4, "林聪", "豹子头");
        final HeroNode node5 = new HeroNode(4, "林zi聪", "豹子头");

        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addByOrder(node2);
        singleLinkedList.addByOrder(node4);
        singleLinkedList.addByOrder(node3);
        singleLinkedList.addByOrder(node1);
//        singleLinkedList.addByOrder(node5);
        singleLinkedList.list();
        /*System.out.println("更新之后的结果");
        singleLinkedList.updateByNo(node5);
        singleLinkedList.list();
        System.out.println("删除之后的结果");
        singleLinkedList.delete(1);
        singleLinkedList.delete(2);
        singleLinkedList.delete(3);
        singleLinkedList.delete(4);
        singleLinkedList.list();*/
        System.out.println("准备反转");
        reverseList(singleLinkedList.getHead());
        singleLinkedList.list();
    }
}

// 定义单链表 管理英雄
class SingleLinkedList {
    // 初始化一个头节点，头节点不能变，不存放具体数据
    private HeroNode head = new HeroNode(0, "", "");

    public HeroNode getHead() {
        return head;
    }

    // add节点到单链表
    public void add(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    //显示
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.next;
        while (temp != null) {
            System.out.println(temp.toString());
            temp = temp.next;
        }
    }

    /**
     * @param node node
     */
    public void addByOrder(HeroNode node) {
        HeroNode temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > node.no) {
                break;
            } else if (temp.next.no == node.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.printf("不能添加，编号%d已存在.\n", node.no);
        } else {
            node.next = temp.next;
            temp.next = node;
        }
    }

    /**
     * 根据no修改信息
     *
     * @param heroNode
     */
    public void updateByNo(HeroNode heroNode) {
        if (head.next == null) {
            System.out.println("链表为空，直接添加");
            addByOrder(heroNode);
        }
        HeroNode temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == heroNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = heroNode.name;
            temp.nickName = heroNode.nickName;
        } else {
            System.out.printf("没有找到编号为%d的hero", heroNode.no);
        }
    }

    public void delete(int no) {
        HeroNode temp = head;
        boolean flag = false;
        if (temp.next == null) {
            System.out.println("空链表，删除失败。");
            return;
        }
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                flag = true;
                break;
            } else {
                temp = temp.next;
            }
        }
        if (flag) {
            temp.next = temp.next.next;
        } else {
            System.out.printf("编号%d未找到\n", no);
        }
    }


}


// 定义HeroNode
class HeroNode {
    public int no;
    public String name;
    public String nickName;
    public HeroNode next;

    public HeroNode(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
